package com.freshcity.page.talk
{
	import com.freshcity.component.container.Component;
	import com.freshcity.core.Dispatcher;
	import com.freshcity.core.MainScene;
	import com.freshcity.core.OpCodes;
	import com.freshcity.lib.event.EventName;
	import com.freshcity.manager.EffectManager;
	import com.freshcity.talk.RequestTalkWin;
	import com.freshcity.utils.StringUtil;
	import com.greensock.TweenLite;
	import com.greensock.TweenMax;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import flashx.textLayout.formats.TextAlign;

	public class RequestTalk extends Component
	{
		private var acceptLink:TextField;
		private var rejectLink:TextField;
		private var owner:*;
		private var uid:String;
		private var back:RequestTalkWin;

		public function RequestTalk(owner:*, user:String)
		{
			this.owner=owner;
			this.uid=user;

			super(owner, 0, 0);
			owner.main.commu.register(OpCodes.REJECT_TALK, onReject);

			Dispatcher.addEventListener(EventName.StartTalk, hide);
			back.cancelBtn.addEventListener(MouseEvent.CLICK, hide);
			back.submitBtn.addEventListener(MouseEvent.CLICK, onSubmit);
		}

		protected function onSubmit(event:MouseEvent):void
		{
			var c:String=StringUtil.trim(back.words.text);
			owner.main.commu.send(OpCodes.REQ_TALK, {u: this.uid, f: int(back.flowerAmt.value), w: c});
			back.cancelBtn.removeEventListener(MouseEvent.CLICK, hide);
			back.submitBtn.removeEventListener(MouseEvent.CLICK, onSubmit);

			back.bg.height=80;
			back.gotoAndStop(2);
			resetPostion();
			back.reqTip.text="正请求与用户" + uid + "聊天...";
			//居中显示信息>>"正请求与用户XX聊天"，锁屏幕5秒钟
			back.closeBtn.addEventListener(MouseEvent.CLICK,hide);
		}

		private function onReject(data:*):void
		{
			var msg:String="用户" + uid + "委婉拒绝了聊天请求。";
			back.gotoAndStop(3);
			//resetPostion();
			owner.main.msg.showFixRollMsg(msg);
			back.closeBtn.addEventListener(MouseEvent.CLICK,hide);
		}

		override public  function hide(event:Event=null):void
		{
			if(back.closeBtn)back.closeBtn.removeEventListener(MouseEvent.CLICK,hide);
			TweenMax.to(this, 0.5, {autoAlpha: 0, y: "+20", onComplete: destroy});
		}

		override public function destroy():void
		{
			owner.main.commu.unRegister(OpCodes.REJECT_TALK);
			Dispatcher.removeEventListener(EventName.StartTalk, hide);
			if (this.parent)
				this.parent.removeChild(this);
		}

		override protected function addChildren():void
		{
			super.addChildren();
			back=new RequestTalkWin();
			addChild(back);
			back.words.text="你好，有缘此刻遇到，聊聊天可以吗？";
		}

		private function resetPostion():void
		{
			this.x=(owner.width - back.width) / 2;
			this.y=(owner.height - back.height) / 2;
		}

		override public function draw():void
		{
			super.draw();
			resetPostion();
			back.gotoAndStop(1);
		}
	}
}